//
//  MessageBaseCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/21.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "MessageBaseCell.h"

@interface MessageBaseCell ()

@property (nonatomic, strong) MASConstraint *leftMsgContentLayout;
@property (nonatomic, strong) MASConstraint *rightMsgContentLayout;
@property (nonatomic, strong) MASConstraint *leftIconLayout;
@property (nonatomic, strong) MASConstraint *rightIconLayout;
@property (nonatomic, strong) MASConstraint *leftSendingLayout;
@property (nonatomic, strong) MASConstraint *rightSendingLayout;

@end

@implementation MessageBaseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self baseSetupUI];
        [self baseBindDatas];
    }
    return self;
}
- (void)baseBindDatas {
    @weakify(self);
    [RACObserve(self, msgModel.msg.sendStatus) subscribeNext:^(id  _Nullable x) {
        SendStatus status = [x integerValue];
        @strongify(self);
        if (status == SendStatus_failed) {
            self.sendFailedBtn.hidden = NO;
            [self.sendingView stopAnimating];
        } else if (status == SendStatus_sending) {
            self.sendFailedBtn.hidden = YES;
            [self.sendingView startAnimating];
        } else {
            self.sendFailedBtn.hidden = YES;
            [self.sendingView stopAnimating];
        }
    }];
    [self.rac_prepareForReuseSignal subscribeNext:^(RACUnit * _Nullable x) {
        @strongify(self);
        [self.sendingView stopAnimating];
        self.sendFailedBtn.hidden = YES;
    }];
}
- (void)baseSetupUI {
    
    self.iconImgView = UIImageView.new;
    self.iconImgView.userInteractionEnabled = YES;
    self.iconImgView.image = UIImage.k_imgName(@"placeholder_user");
    self.iconImgView.layer.cornerRadius = Chat_Icon_Size/2;
    self.iconImgView.layer.masksToBounds = YES;
    
    self.nicknameLable = [UILabel k_labelWithFontSize:15 textColor:UIColor.k_t2Color];
    self.msgContentView = [[MsgContentView alloc] initWithFrame:self.bounds];
    self.sendingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.sendFailedBtn = UIButton.k_initType(UIButtonTypeCustom).k_img(UIImage.k_imgName(@"msg_send_failed"));
    
    [self.pContentView addSubview:self.iconImgView];
    [self.pContentView addSubview:self.nicknameLable];
    [self.pContentView addSubview:self.msgContentView];
    [self.pContentView addSubview:self.sendingView];
    [self.pContentView addSubview:self.sendFailedBtn];
    
    UITapGestureRecognizer *tapIconG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(base_tapIconImgViewAction:)];
    UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(base_tapMsgContentViewAction:)];
    UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(base_longPressMsgContentiViewAction)];
    [self.iconImgView addGestureRecognizer:tapIconG];
    self.msgContentViewTapGR = tapG;
    [self.msgContentView addGestureRecognizer:tapG];
    [self.msgContentView addGestureRecognizer:longPressG];
    
    [self setupLayout];
    self.msgContentView.needUpdateMask = YES;
}
- (void)base_tapMsgContentViewAction:(UITapGestureRecognizer *)tapG {
    if (self.tapMessageAction) {
        self.tapMessageAction();
    }
}
- (void)base_longPressMsgContentiViewAction {
    if (self.longPressMessageAction) {
        self.longPressMessageAction();
    }
}
- (void)base_tapIconImgViewAction:(UITapGestureRecognizer *)tapG {
    if (self.tapIconAction) {
        self.tapIconAction(self.msgDirection == MsgDirection_send);
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.msgContentView.needUpdateMask = YES;
}

- (void)setupLayout {
    
    [self.iconImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        self.rightIconLayout = make.right.mas_offset(-Chat_Icon_Margin_Left);
        self.leftIconLayout = make.left.mas_offset(Chat_Icon_Margin_Left);
        make.top.mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(Chat_Icon_Size, Chat_Icon_Size));
    }];
    [self.rightIconLayout deactivate];
    [self.leftIconLayout deactivate];
    [self.sendingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.msgContentView);
        self.leftSendingLayout = make.right.equalTo(self.msgContentView.mas_left).offset(-Chat_Status_Margin_Right);
        self.rightSendingLayout = make.left.equalTo(self.msgContentView.mas_right).offset(Chat_Status_Margin_Right);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.leftSendingLayout deactivate];
    [self.rightSendingLayout deactivate];
    [self.sendFailedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.sendingView);
    }];
    
    [self.msgContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.bottom.mas_offset(-10);
        self.leftMsgContentLayout = make.left.equalTo(self.iconImgView.mas_right).offset(Chat_Icon_Margin_Right);
        self.rightMsgContentLayout = make.right.equalTo(self.iconImgView.mas_left).offset(-Chat_Icon_Margin_Right);
    }];
    [self.leftMsgContentLayout deactivate];
    [self.rightMsgContentLayout deactivate];
}

- (void)setMsgDirection:(int)msgDirection {
    if (_msgDirection == msgDirection) {
        return;
    }
    _msgDirection = msgDirection;
    self.msgContentView.msgDirection = msgDirection;
    if (msgDirection == 1) {
        
        [self.leftIconLayout deactivate];
        [self.rightIconLayout activate];
        
        [self.leftMsgContentLayout deactivate];
        [self.rightMsgContentLayout activate];
        
        [self.leftSendingLayout activate];
        [self.rightSendingLayout deactivate];
        
    } else if (msgDirection == 2) {
        
        [self.leftIconLayout activate];
        [self.rightIconLayout deactivate];
        
        [self.leftMsgContentLayout activate];
        [self.rightMsgContentLayout deactivate];
        
        [self.leftSendingLayout deactivate];
        [self.rightSendingLayout activate];
    }
}

- (void)setMsgModel:(MsgModel *)msgModel {
    _msgModel = msgModel;
    
    self.msgDirection = msgModel.direction;
    if (self.msgDirection == MsgDirection_send) {
        [self.iconImgView sd_setImageWithURL:kUsermanager.me.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
    } else {
        
        [kUsermanager getFriendWithUserId:msgModel.msg.send_user_id groupId:msgModel.msg.group_id callbackBlock:^(FriendItem * _Nonnull friendItem) {
            [self.iconImgView sd_setImageWithURL:friendItem.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        }];
    }
}

@end
